#include <stdlib.h>
#include "DevApplication.h"
#include "OptionParser.h"
#include "Version.h"
#include "TStringUtil.h"
#include "Path.h"
#include "AppConst.h"

#ifdef WIN32
#include "CrashDump.h"
#else
#endif 


void setupPath(const char* appPath)
{
    std::string workdir;
    workdir = comn::Path::getWorkDir();
    AppConst::setAppDir(workdir);
}

int main(int argc, char *argv[])
{

    OptionParser parser;
    parser.setUsage("%prog [option]");
    std::string strVersion = comn::StringUtil::format("%s Version %s\nCopyright (c) 2018, %s",
        APP_NAME, APP_VERSION, APP_VENDOR);
    parser.setVersion(strVersion);
    parser.enableHelp();
    parser.enableVersion();
    parser.addOption('d', "daemon", "run as daemon");

    if (!parser.parse(argc, argv))
    {
        parser.usage();
        return -1;
    }

    if (parser.hasHelpOption())
    {
        parser.usage();
        return 0;
    }

    if (parser.hasVersionOption())
    {
        parser.version();
        return 0;
    }

#ifdef WIN32
    cd_setup(4, "CrashReport.bat");
#endif //


    bool isDaemon = parser.hasOption("daemon");

    setupPath(argv[0]);

	//printf("appdir: %s\n", AppConst::getAppDir().c_str());

	DevApplication app;

    app.setDaemon(isDaemon);
    app.launch(argc, argv);

    return EXIT_SUCCESS;
}
